﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.MsgEvents.AccountingReport;
using 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork.MenuPage;

namespace 基于WPF的电缆采样数据分析系统.Views.TDRWork.MenuPage
{
    /// <summary>
    /// LogPageUC.xaml 的交互逻辑
    /// </summary>
    public partial class LogPageUC : UserControl
    {
        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        public LogPageUC()
        {
            InitializeComponent();

            Aggregator = App.GetService<IEventAggregator>();
            Aggregator.GetEvent<MsgScrollToTheLastLine>().Subscribe(ScrollToLastRow);
            Aggregator.GetEvent<MsgSelectTheColumnsToDisplay>().Subscribe(SelectTheColumnsToDisplay);

            this.DataContext = LogPageUCViewModel.Instance;
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HeaderCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < LogPageUCViewModel.Instance.UsedLogList.Count; i++)
            {
                LogPageUCViewModel.Instance.UsedLogList[i].IsSelected = true;
            }
        }

        /// <summary>
        /// 全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HeaderCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < LogPageUCViewModel.Instance.UsedLogList.Count; i++)
            {
                LogPageUCViewModel.Instance.UsedLogList[i].IsSelected = false;
            }
        }

        /// <summary>
        /// 回到最后一行
        /// </summary>
        public void ScrollToLastRow()
        {
            if (WorkLog.Items.Count > 0)
            {
                var lastItem = WorkLog.Items[WorkLog.Items.Count - 1];
                WorkLog.ScrollIntoView(lastItem);
                WorkLog.UpdateLayout(); // 确保滚动立即生效

                // 更新报表分析区的操作日志信息
                Aggregator.GetEvent<MsgUpdateUsedLogList>().Publish();
            }
        }


        /// <summary>
        /// 选择列显示
        /// </summary>
        private void SelectTheColumnsToDisplay(int column)
        {
            switch (column)
            {
                case 0:
                    Aid.Visibility = Visibility.Visible;
                    break;
                case 1:
                    Aid.Visibility = Visibility.Hidden;
                    break;
                case 2:
                    Uid.Visibility = Visibility.Visible;
                    break;
                case 3:
                    Uid.Visibility = Visibility.Hidden;
                    break;
                case 4:
                    Time.Visibility = Visibility.Visible;
                    break;
                case 5:
                    Time.Visibility = Visibility.Hidden;
                    break;
                case 6:
                    OperateDescription.Visibility = Visibility.Visible;
                    break;
                case 7:
                    OperateDescription.Visibility = Visibility.Hidden;
                    break;

            }
        }
    }
}
